package services.common;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import models.common.entity.t_credit_record;
import services.base.BaseService;

import common.utils.Factory;
import common.utils.PageBean;
import common.utils.ResultInfo;

import daos.common.CreditRecordDao;

/**
 * 信用积分记录 实现service
 * @author zhiliang.lan
 *	2017年5月2日
 *
 *	zhiliang.lan
 */
public class CreditRecordService extends BaseService<t_credit_record>{
	
	protected CreditRecordDao creditRecordDao = Factory.getDao(CreditRecordDao.class);

	protected CreditRecordService() {
		super.basedao = this.creditRecordDao;
	}
	
	/**
	 * 保存新增
	 * @param creditRecord
	 * @return
	 * @author zhiliang.lan
	 * @createDate 2017年5月2日
	 */
	public ResultInfo addCreditRecord(t_credit_record creditRecord) {
		ResultInfo result = new ResultInfo();
		
		boolean flag = creditRecordDao.save(creditRecord);
		if (!flag) {
			
			result.code = ResultInfo.ERROR_SQL;
			result.msg = "系统错误";	
			
			return result;
		} 
		
		result.code = 1;
		result.msg="交易信用积分添加成功!";
		result.obj = creditRecord;
		
		return result;
	}

	/**
	 * 查询企业信用积分记录
	 * @param companyInfoId
	 * @return
	 * @author zhiliang.lan
	 * @createDate 2017年6月7日
	 */
	public List<t_credit_record> findListByCompanyInfoId(long userId) {
		
		return creditRecordDao.findListByCompanyInfoId(userId);
	}
	
	/**
	 * 查询企业信用积分记录,只查询直接增加的
	 * @param companyInfoId
	 * @return
	 * @author zhiliang.lan
	 * @createDate 2017年7月14日14:19:30
	 * 
	 */
	public List<t_credit_record> findDirectListByUserId(long userId) {
		
		return creditRecordDao.findDirectListByUserId(userId);
	}

	/**
	 * 分页查询企业评估资料首页  积分记录  -app
	 * @param userId
	 * @param currPage
	 * @param pageSize
	 * @return
	 * @author zhiliang.lan
	 * @createDate 2017年8月4日
	 */
	public PageBean<Map<String, Object>> findDirectListByUserIdApp(long userId,
			int currPage, int pageSize) {
		
		return creditRecordDao.findDirectListByUserIdApp(userId, currPage, pageSize);
	}

	/**
	 * 分页查询积分记录表
	 * @param userId 用户ID
	 * @param currPage
	 * @param pageSize
	 * @return
	 * @author Liangqiongfeng
	 *@version 2018-1-19 
	 */
	public PageBean<t_credit_record> pageByUserId(long userId, int currPage, int pageSize) {
		StringBuffer querySQL = new StringBuffer(" SELECT id,credit_score,time,user_id,reason,credit_score_type,subject,pic_id FROM t_credit_record WHERE 1 = 1 ");
		StringBuffer countSQL = new StringBuffer(" SELECT COUNT(*) FROM	t_credit_record WHERE 1 = 1 ");	
		
		Map<String, Object> condition = new HashMap<String, Object>();
		if(userId > 0){
			querySQL.append(" AND user_id = :userId");
			countSQL.append(" AND user_id = :userId");
			condition.put("userId", userId);
		}
		querySQL.append(" ORDER BY time DESC");
		countSQL.append(" ORDER BY time DESC");
		return creditRecordDao.pageOfBySQL(currPage, pageSize, countSQL.toString(), querySQL.toString(), condition);
	}
	
}
